package com.sgb.link.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.link.R;
import com.sgb.link.databinding.ItemOneAndTwoChoiceBinding;
import com.sgb.link.databinding.ItemPickFilterChildWindow1Binding;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class OneAndTwoLevelMultipleChoiceWindow extends BasePopWindow {

    private String title;
    private TextView tvTitle;
    private RecyclerView recycler1;
    private RecyclerView recycler2;

    private ChooseAdapter chooseAdapterOne;
    private ChooseAdapterTwo chooseAdapterTwo;

    private int mLastPosition = -1;

    private int mFirstChoosePosition = -1; //当前选中的一级

    private List<PickFilterEntity> pickFilterEntityList = new ArrayList<>();//选中的数据

    public OneAndTwoLevelMultipleChoiceWindow(Context context) {
        super(context, DIR_DOWN_UP);
        pickFilterEntityList.clear();
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_multiple_choice;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recycler1 = holder.getPop().findViewById(R.id.rlv_list);
        recycler2 = holder.getPop().findViewById(R.id.rlv2_list);
        TextView cancel = holder.getPop().findViewById(R.id.cancel);
        tvTitle = holder.getPop().findViewById(R.id.title);
        TextView confirm = holder.getPop().findViewById(R.id.confirm);

        //取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });

        //确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    if (pickFilterEntityList.isEmpty()){
                        MToast.showToast(mContext, "请选择数据后再确认");
                        return;
                    }
                    if (pickFilterEntityList.size() > 20){
                        MToast.showToast(mContext, "最多20条");
                        return;
                    }
                    onResultClick.result(pickFilterEntityList);
                    mLastPosition = -1;
                    dissMiss();

                }
            }
        });
        if (chooseAdapterOne == null){
            chooseAdapterOne = new ChooseAdapter(R.layout.item_one_and_two_choice);
        }
        if (chooseAdapterTwo == null){
            chooseAdapterTwo = new ChooseAdapterTwo(R.layout.item_pick_filter_child_window_1);
        }
        recycler1.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        recycler1.setAdapter(chooseAdapterOne);
        XLinearLayoutManager manager1 = new XLinearLayoutManager(mContext);
        recycler2.setLayoutManager(manager1);
        recycler2.setAdapter(chooseAdapterTwo);

        chooseAdapterOne.setClickItemCallBack(new ClickItemCallBack() {

            @Override
            public void chooseItem(PickFilterEntity entity, int position) {
                if (!pickFilterEntityList.contains(entity)){
                    pickFilterEntityList.add(entity);
                } else {
                    pickFilterEntityList.remove(entity);
                }
                chooseAdapterOne.notifyItemChanged(position);
            }

            @Override
            public void clickItem(PickFilterEntity<List<PickFilterEntity>> entity, int position) {
                mFirstChoosePosition = position;
                chooseAdapterTwo.setNewData(entity.getChildren());
                List<PickFilterEntity> data = chooseAdapterOne.getData();
                if (entity.isClickItem()) return;
                if (mLastPosition > -1 && mLastPosition < data.size()){
                    data.get(mLastPosition).setClickItem(false);
                    chooseAdapterOne.notifyItemChanged(mLastPosition);
                }
                if (position > -1 && position < data.size()){
                    data.get(position).setClickItem(true);
                    chooseAdapterOne.notifyItemChanged(position);
                }
                mLastPosition = position;
            }
        });

        chooseAdapterTwo.setClickItemCallBack(new ClickItemCallBack() {
            @Override
            public void chooseItem(PickFilterEntity<List<PickFilterEntity>> entity, int position) {

            }

            @Override
            public void clickItem(PickFilterEntity<List<PickFilterEntity>> entity, int position) {
                if (!pickFilterEntityList.contains(entity)){
                    pickFilterEntityList.add(entity);
                    } else {
                    pickFilterEntityList.remove(entity);
                }
                chooseAdapterTwo.getData().get(position).setChoose(!entity.isChoose());
                chooseAdapterTwo.notifyItemChanged(position);
                chooseOneItem(chooseAdapterTwo.getData());
            }
        });

    }

    private void chooseOneItem(List<PickFilterEntity> data) {
        if (data == null || data.isEmpty()) return;
        boolean isReset = true;
        for (PickFilterEntity entity : data) {
            if (entity.isChoose()) {
                if (mFirstChoosePosition != -1) {
                    if (mFirstChoosePosition >= 0 && mFirstChoosePosition < chooseAdapterOne.getData().size()) {
                        isReset = false;
                        chooseAdapterOne.getData().get(mFirstChoosePosition).setCheckItem(true);
                        chooseAdapterOne.notifyItemChanged(mFirstChoosePosition);
                    }
                }
                break;
            }
        }
        if (isReset) {
            if (mFirstChoosePosition != -1) {
                if (mFirstChoosePosition >= 0 && mFirstChoosePosition < chooseAdapterOne.getData().size()) {
                    chooseAdapterOne.getData().get(mFirstChoosePosition).setCheckItem(false);
                    chooseAdapterOne.notifyItemChanged(mFirstChoosePosition);
                }
            }
        }
        //mFirstChoosePosition = -1;
    }


    public void setTitle(String title) {
        this.title = title;
        if (tvTitle != null && !TextUtils.isEmpty(title))
            tvTitle.setText(title);
    }


    public void setHistory(List<PickFilterEntity<List<PickFilterEntity>>> list, List<PickFilterEntity> historyDatas){
        pickFilterEntityList.clear();
        if (historyDatas != null){
            pickFilterEntityList.addAll(historyDatas);
        }
        if (!pickFilterEntityList.isEmpty()) {
            if (list != null && !list.isEmpty()) {
                for (PickFilterEntity<List<PickFilterEntity>> entity : list) {
                    if (entity.getChildren() != null && !entity.getChildren().isEmpty()) {
                        for (PickFilterEntity entity1 : entity.getChildren()) {
                            if (pickFilterEntityList.contains(entity1)) {
                                entity.setCheckItem(true);
                                break;
                            }
                        }
                    }
                }
            }
        }
        setData(list);
    }


    private OnResultClick onResultClick;


    public interface OnResultClick {

        void result(List<PickFilterEntity> list);
    }

    public void setOnResultClick(OnResultClick onResultClick) {
        this.onResultClick = onResultClick;
    }


    interface ClickItemCallBack{
        void chooseItem(PickFilterEntity<List<PickFilterEntity>> entity, int position);
        void clickItem(PickFilterEntity<List<PickFilterEntity>> entity, int position);
    }

    public void setData(List<PickFilterEntity<List<PickFilterEntity>>> list) {
        List<PickFilterEntity> pickList = new ArrayList<>();
        for (PickFilterEntity<List<PickFilterEntity>> pickFilterEntity : list) {
            PickFilterEntity entity = new PickFilterEntity();
            entity.setNo(pickFilterEntity.getNo());
            entity.setName(pickFilterEntity.getName());
            entity.setId(pickFilterEntity.getId());
            entity.setChildren(pickFilterEntity.getChildren());
            entity.setCheckItem(pickFilterEntity.isCheckItem());
            pickList.add(entity);
        }
        pickList.get(0).setClickItem(true);
        mLastPosition = 0;
        mFirstChoosePosition = 0;
        chooseAdapterOne.setNewData(pickList);
        recycler1.scrollToPosition(0);
        chooseAdapterTwo.setNewData((List<PickFilterEntity>) pickList.get(0).getChildren());
        recycler2.scrollToPosition(0);
    }

    class ChooseAdapter extends BaseQuickAdapter<PickFilterEntity, BaseViewHolder> {
        private ClickItemCallBack mCallBack;
        private ItemOneAndTwoChoiceBinding mItemBinding;
        public ChooseAdapter(int layoutResId) {
            super(layoutResId);
        }


        public void setClickItemCallBack(ClickItemCallBack callBack) {
            this.mCallBack = callBack;
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, PickFilterEntity item) {
            mItemBinding = DataBindingUtil.bind(holder.itemView);
            mItemBinding.setViewModel(item);
            mItemBinding.iv.setImageResource(pickFilterEntityList.contains(item) ? R.drawable.icon_one_choose : R.drawable.shape_rd2_bd1_grey_bg);
            mItemBinding.llContent.setSelected(item.isClickItem());
            mItemBinding.tvSelectContent.setTextColor(mContext.getResources().getColor(item.isCheckItem() ? R.color.color_main : R.color.color_title_medium));
            holder.itemView.setOnClickListener(v -> {
                if (mCallBack != null ){
                    mCallBack.clickItem(item, holder.getLayoutPosition());
                }
            });
            mItemBinding.llIv.setOnClickListener(v -> {
                if (mCallBack != null ){
                    mCallBack.chooseItem(item, holder.getLayoutPosition());
                }
            });

        }


    }

    class ChooseAdapterTwo extends BaseQuickAdapter<PickFilterEntity, BaseViewHolder> {
        private ClickItemCallBack mCallBack;
        private ItemPickFilterChildWindow1Binding mItemBinding;
        public ChooseAdapterTwo(int layoutResId) {
            super(layoutResId);
        }

        public void setClickItemCallBack(ClickItemCallBack callBack) {
            this.mCallBack = callBack;
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, PickFilterEntity item) {
            mItemBinding = DataBindingUtil.bind(holder.itemView);
            mItemBinding.setViewModel(item);
            TextView picker = holder.itemView.findViewById(R.id.tv_selected);
            TextView tvName = holder.itemView.findViewById(R.id.tv_select_content);

            tvName.getPaint().setFakeBoldText(pickFilterEntityList.contains(item));
            picker.setSelected(pickFilterEntityList.contains(item));


            holder.itemView.setOnClickListener(v -> {
                if (mCallBack != null){
                    mCallBack.clickItem(item, holder.getLayoutPosition());
                }
            });
        }


    }


}
